<?php
session_start();
require_once("../Info.php");
require_once("../Artist.php");
require_once("VideoItem.php");
require_once("Mtvmusic.php");

if(empty($_SESSION['played']))
    $_SESSION['played'] = array();
if(!empty($_GET['username']))
    $username = urlencode($_GET['username']);
else 
    $username = "plazma";
        
$xml = file_get_contents("http://ws.audioscrobbler.com/2.0/?method=user.gettopartists&user=".$username."&api_key=519681edb551a3b5177bb67352e16136"); 
$sxml = new SimpleXMLElement($xml);
$sxml = $sxml->topartists;
foreach($sxml->artist as $obj) {
    $artist = new Artist($obj->name);
    $artist->cachePathSimiliarArtist = "../cache/artist/similar/"; //use of default absolute path could avoid this
    $similarArtists = $artist->getSimilarArtits(5);
    if(!empty($artist))
        $artistArr[] = $artist;
    foreach($similarArtists as $sas) {
        if(!empty($sas))
            $artistArr[] = $sas;
    } 
} 

$playlist = array();
foreach($artistArr as $artist) {
    $Mtvmusic = new Mtvmusic();
    $artistPlaylist = $Mtvmusic->getVideoItems($artist->name);
    if(!empty($artistPlaylist)) {
        $playlist = array_merge($playlist, $artistPlaylist);
    }
}
shuffle($playlist);

//Info::dump($_SESSION['played']);die;

foreach($playlist as $p) {
    if(!in_array($p->id, $_SESSION['played'])) {
        $play = $p;
        break;
    }
}
if(!empty($play)) {
    $_SESSION['played'][] = (string)$play->id; 
    
    echo $play->artistName." - ".$play->title;
    ?>
    | <a href="index.php">Next</a><br/>
    <meta http-equiv="refresh" content="<?=$play->duration ?>;url=index.php">
    <embed src="<?=$play->url ?>"
         width="800" height="600"
         type="application/x-shockwave-flash"
         allowFullScreen="true"
         allowScriptAccess="always"
         flashvars="autoPlay=true" />
    <? 
    
    
    //Print debug info
    echo "<h2>Debug info</h2>";
    echo "<b>Session list of played videos</b>";
    Info::dump($_SESSION['played']);
    echo "<br/><br/><b>\$play</b>";
    Info::dump($play);
    echo "<b>\$playlist</b>";
    Info::dump($playlist); 
}
?>
